package com.antu.first.service.impl;

import com.antu.first.bean.*;
import com.antu.first.dao.*;
import com.antu.first.dto.HotelConditions;
import com.antu.first.dto.HotelOperationExecution;
import com.antu.first.dto.ImageHolder;
import com.antu.first.enums.HotelStateEnum;
import com.antu.first.exceptions.HotelOperationException;
import com.antu.first.service.HotelService;
import com.antu.first.util.ImageHandle;
import com.antu.first.util.ImageUtil;
import com.antu.first.util.PathUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * Created by 龙程 on 2018/3/2.
 */
@Service("hotelService")
public class HotelServiceImpl implements HotelService{
    @Autowired
    private HotelDao hotelDao;
    @Autowired
    SwiperDao swiperDao;
    @Autowired
    private RoomTypeDao roomTypeDao;
    @Autowired
    private RoomDao roomDao;
    @Autowired
    DiscountCouponDao discountCouponDao;
    boolean flag;
    Map<String, Object> map;
    List<Hotel>hotelList;
    List<Swiper>swiperList;
    Hotel hot;

    private void addHotelImg(Hotel hotel, ImageHolder hotelImg){
        String dest = null;
        if(hotel != null && hotel.gethId() != 0){
            dest = PathUtil.getHotelImgPath(hotel.gethId());
        }else{
            dest = PathUtil.getHotelImgPath(1011);
        }
        String imgAddr = ImageUtil.generateNormalImg(hotelImg, dest);
        hotel.sethProfile(imgAddr);
    }

    @Override
    @Transactional
    public HotelOperationExecution updateHotelInfo(Hotel hotel, ImageHolder hotelImg) {
        if(hotel.gethId()!=0&&hotel.gethName()!=null&&hotel.gethAddr()!=null&&hotel.gethTel()!=null&& hotel.gethDesc()!=null){
            try{
                hotel.sethLastEditTime(new Date());
                if(hotelImg != null){
                    try{
                        Hotel tempHotel = hotelDao.selectHotelById(hotel.gethId());
                        if(tempHotel != null && tempHotel.gethProfile() != null){
                            ImageUtil.deleteFileOrPath(tempHotel.gethProfile());
                        }
                        addHotelImg(hotel, hotelImg);
                        if(hotel.gethProfile() == null && "".equals(hotel.gethProfile())){
                            return new HotelOperationExecution(HotelStateEnum.EMPTY_ADDR);
                        }
                    }catch(Exception e){
                        throw new HotelOperationException("HotelServiceImpl-updateHotelInfo，在修改酒店缩略图的过程中发生错误："+e.getMessage());
                    }
                }
                int effectNum = hotelDao.updateHotelInfo(hotel);
                if (effectNum <= 0) {
                    return new HotelOperationExecution(HotelStateEnum.UPDATE_FAILED);
                } else {
                    return new HotelOperationExecution(HotelStateEnum.UPDATE_SUCCESS);
                }
            }catch(Exception e){
                throw new HotelOperationException("HotelServiceImpl-updateHotelInfo，在修改酒店基础信息的过程中发生错误："+e.getMessage());
            }
        }else {
            return new HotelOperationExecution(HotelStateEnum.EMPTY_IMAGE);
        }
    }

    /**
     *
     * @param
     * @return
     */
    @Override
    public Hotel queryHotelByName(int mId) {
        return hotelDao.queryHotelByName(mId);
    }

    /**
     * 手机端主页搜索酒店，和进入首页
     * @param hotelConditions
     * @return
     */
    @Override
    public Map<String, Object> searchHotel(HotelConditions hotelConditions) {
        map=new HashMap<String,Object>();
        hotelList=new ArrayList<>();
        swiperList=new ArrayList<>();
        hotelList=hotelDao.searchHotel(hotelConditions);
        swiperList=swiperDao.toPromoteSwiper(1);//手机端调用此方法，是查询被授权的轮播图集合，管理员调用1为授权，0为未授权
        map.put("swiperList",swiperList);
        map.put("hoteList",hotelList);
        return map;
    }

    /**
     * 移动端按照酒店主键，获得酒店相关信息
     * @param hotelId
     * @return
     */
    @Override
    public Map<String, Object> hotelInformation(Integer hotelId) {
        map=new HashMap<String,Object>();
        swiperList=new ArrayList<>();
        hotelList=new ArrayList<>();
        List<DiscountCoupon>discountCouponList=new ArrayList<>();
        hot=new Hotel();
        hot.sethId(hotelId);
        DiscountCoupon discountCoupon=new DiscountCoupon();
        discountCoupon.setHotel(hot);
        discountCoupon.setdStatus(2);
        swiperList=swiperDao.selectSwipersByHotelId(hotelId);//查询被启用了的轮播图
        List<Room> rooms = roomDao.selectRoomsByHotelId(hotelId);//查询所有的房间
        discountCouponList=discountCouponDao.selectByHotelIdAndCouponId(discountCoupon);//查询被发布并且启用了的优惠券
        List<RoomType> roomTypes = roomTypeDao.queryRoomTypeByHid(hotelId);
        hot=hotelDao.selectHotelById(hotelId);
        hot.setRoomTypes(roomTypes);
        hot.setRooms(rooms);
        map.put("swiperList",swiperList);
        map.put("hotel",hot);
        map.put("discountCouponList",discountCouponList);
        return map;
    }

    @Override
    public List<Hotel> selectHotelName() {
        return hotelDao.selectHotelName();
    }

}